package com.example.app.network.api

import com.example.app.data.bean.AccountCat
import com.example.app.data.bean.AccountTitle
import com.example.app.data.bean.BaseResponseData
import com.example.app.data.bean.routeAccount.AccountRecord
import com.example.app.data.bean.routeAccount.AccountRecordPageData
import com.example.app.data.bean.routeAccount.BudgetBalanceOfMonthData
import com.example.app.data.bean.routeAccount.BudgetData
import com.example.app.data.bean.routeAddAccount.AccountRelationPeople
import com.example.app.data.bean.routeAddAccount.TitlesAndCats
import com.example.app.data.bean.routeAddAccount.UploadFile
import com.example.app.data.enum.AccountRecordState
import com.example.app.data.enum.PostFileTypeState
import com.example.app.network.createWebService
import com.example.app.util.CacheUtil
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import retrofit2.http.*

interface AccountService {
    companion object {
        var service = createWebService(
            ServiceConfig.SERVER_URL,
            AccountService::class.java
        )
    }
    //登录

    //获取标题和分类
    @GET("account/titlesAndCats")
    suspend fun titlesAndCats(
        @Query("CreatorId") CreatorId: String = CacheUtil.getUserId(),
        @Query("AccountType") AccountType: Boolean? = null,
    ): BaseResponseData<TitlesAndCats>
    //获取资源文件
    @GET("file/resource/{type}")
    suspend fun getResource(
        @Path("type") type: String,
        @Query("action") action: String? = null
    ): BaseResponseData<List<String>>
    //获取账目关联人员
    @GET("account/accountRelation")
    suspend fun accountRelation(
        @Query("CreatorId") CreatorId: String? = CacheUtil.getUserId(),
    ): BaseResponseData<List<AccountRelationPeople>>
    //删除账户关联人
    @FormUrlEncoded
    @POST("account/removeAccountRelationPeople")
    suspend fun removeAccountRelationPeople(
        @Field("CreatorId") CreatorId: String? = CacheUtil.getUserId(),
        @Field("DefineType") DefineType: String,
        @Field("AccountRelationId") AccountRelationId: String,
    ): BaseResponseData<AccountRelationPeople>
    //新增账户关联人
    @FormUrlEncoded
    @POST("account/addAccountRelationPeople")
    suspend fun addAccountRelationPeople(
        @Field("CreatorId") CreatorId: String? = CacheUtil.getUserId(),
        @Field("Remark") Remark: String? = null,
        @Field("Title") Title: String,
    ): BaseResponseData<AccountRelationPeople>
    //修改账户关联人
    @FormUrlEncoded
    @POST("account/updateAccountRelationPeople")
    suspend fun updateAccountRelationPeople(
        @Field("CreatorId") CreatorId: String? = CacheUtil.getUserId(),
        @Field("Title") Title: String,
        @Field("_id") _id: String,
    ): BaseResponseData<AccountRelationPeople>
    //添加一条新的账目
    @FormUrlEncoded
    @POST("account/addAccountRecord")
    suspend fun addAccountRecord(
        @Field("CreatorId") CreatorId: String? = CacheUtil.getUserId(),
        @Field("AccountType") AccountType: Boolean? = null,
        @Field("CatId") CatId: String? = null,
        @Field("TitleName") TitleName: String? = null,
        @Field("TitleId") TitleId: String? = null,
        @Field("Money") Money: String? = null,
        @Field("Place") Place: String? = null,
        @Field("Remark") Remark: String? = null,
        @Field("Date") Date: String? = null,
        @Field("IconPath") IconPath: String? = null,
        @Field("AccountRelationPeopleId") AccountRelationPeopleId: String? = null,
        @Field("FileIds") FileIds: List<String>? = null,
    ): BaseResponseData<Any>
    /**
     * 查询条目
     */
    @FormUrlEncoded
    @POST("account/getAccountRecord")
    suspend fun getAccountRecord(
        @Field("CreatorId") CreatorId: String? = CacheUtil.getUserId(),
        @Field("AccountRelationPeopleId") AccountRelationPeopleId: String? = null,
        @Field("AccountType") AccountType: String? = null,
        @Field("w") w: String? = null,//搜索用
        @Field("getAccountRecordType") getAccountRecordType: String? = AccountRecordState.ACCOUNT_RECORD_GET.value.toString(),//搜索用
        @Field("pageNum") pageNum: Int,
        @Field("pageSize") pageSize: Int,
        //传入数组字符串，例如 arrayList("2022-8-8","2022-9-3")
        @Field("dateRange") dateRange: List<String>? = null,
    ): BaseResponseData<AccountRecordPageData<AccountRecord>>
    /*删除一条账目*/
    @DELETE("account/remove")
    suspend fun accountRemoveOne(
        @Query("CreatorId") CreatorId: String? = CacheUtil.getUserId(),
        @Query("_id") _id: String,
    ): BaseResponseData<Any>
    //查询账户详情
    @GET("account/recordDetail")
    suspend fun recordDetail(
        @Query("CreatorId") UserId: String? = CacheUtil.getUserId(),
        @Query("recordId") recordId: String,
    ): BaseResponseData<AccountRecord>
    //查询预算
    //查询账户详情
    @GET("account/budget")
    suspend fun budgetGet(
        @Query("CreatorId") UserId: String? = CacheUtil.getUserId(),
        @Query("budgetMonth") budgetMonth: String?=null,
        @Query("Remark") Remark: String? = null,
        @Query("budget") budget: String? = null,
    ): BaseResponseData<Array<BudgetData>>
    //新增预算
    @FormUrlEncoded
    @POST("account/budget/add")
    suspend fun budgetAdd(
        @Field("CreatorId") CreatorId: String? = CacheUtil.getUserId(),
        @Field("budgetMonth") budgetMonth: String,
        @Field("budget") budget: String? = "",
        @Field("Remark") Remark: String? = "",
    ): BaseResponseData<Any>
    // 本月预算剩余 只做本月预算剩余用,不要拿来查询时间段内的收入和支出.
    // 注意,dateRange传一个月的开始和结束日期,不要跨月
    @FormUrlEncoded
    @POST("account/budget/budgetBalanceOfMonth")
    suspend fun budgetBalanceOfMonth(
        @Field("CreatorId") CreatorId: String? = CacheUtil.getUserId(),
        @Field("budgetMonth") budgetMonth: String,
        //传入数组字符串，例如 arrayList("2022-8-8","2022-9-3")
        @Field("dateRange") dateRange: ArrayList<String>? = null,
    ): BaseResponseData<BudgetBalanceOfMonthData>
    // 新增类别
    @FormUrlEncoded
    @POST("account/addCat")
    suspend fun addCat(
        @Field("CreatorId") CreatorId: String? = CacheUtil.getUserId(),
        @Field("AccountType") AccountType: Boolean,
        @Field("TypeName") TypeName: String? = "",
        @Field("OrderNum") OrderNum: Int? = 0,
        @Field("IconPath") IconPath: String? = "",
    ): BaseResponseData<AccountCat>
    // 新增标题
    @FormUrlEncoded
    @POST("account/addTitle")
    suspend fun addTitle(
        @Field("CreatorId") CreatorId: String? = CacheUtil.getUserId(),
        @Field("AccountType") AccountType: Boolean,
        @Field("CatType") CatType: String? = "",
        @Field("CatId") CatId: String? = "",
        @Field("TitleName") TitleName: String? = "",
        @Field("OrderNum") OrderNum: Int? = 0,
        @Field("IconPath") IconPath: String? = "",
    ): BaseResponseData<AccountTitle>
    //停用cat 或者 title
    //如果是cat , 传catId
    //如果是title 传titleId
    @FormUrlEncoded
    @POST("account/stop")
    suspend fun stopCatOrTitle(
        @Field("CreatorId") CreatorId: String? = CacheUtil.getUserId(),
        @Field("Type") Type: String? = "",
        @Field("CatId") CatId: String? = "",
        @Field("TitleId") TitleId: String? = "",
    ): BaseResponseData<Any>
    //更新cat
    @FormUrlEncoded
    @POST("account/updateCat")
    suspend fun updateCat(
        @Field("CreatorId") CreatorId: String? = CacheUtil.getUserId(),
        @Field("TypeName") TypeName: String? = "",
        @Field("_id") _id: String? = "",
        @Field("IconPath") IconPath: String? = "",
    ): BaseResponseData<AccountCat>
    //更新title
    @FormUrlEncoded
    @POST("account/updateTitle")
    suspend fun updateTitle(
        @Field("CreatorId") CreatorId: String? = CacheUtil.getUserId(),
        @Field("_id") _id: String? = "",
        @Field("TitleName") TitleName: String? = "",
        @Field("IconPath") IconPath: String? = "",
        @Field("CatType") CatType: String? = "",
        @Field("CatId") CatId: String? = "",
    ): BaseResponseData<AccountTitle>
    //查询cat 或者 title
    @GET("account/str")
    suspend fun searchCatOrTitle(
        @Query("CreatorId") UserId: String? = CacheUtil.getUserId(),
        @Query("w") w: String,
        @Query("accountRecordType") accountRecordType: Boolean? = null,
    ): BaseResponseData<Any>
    /*
    * 多文件上传
    * */
    @POST("/file/uploadFiles")
    @Multipart
    suspend fun uploadFiles(
        @Part("CreatorId") CreatorId: RequestBody? = CacheUtil.getUserId()
            .toRequestBody("multipart/form-data".toMediaTypeOrNull()),
        @Part("PostFileType") PostFileType: RequestBody? = PostFileTypeState.POST_FILE_TYPE_IMG.value.toString()
            .toRequestBody("multipart/form-data".toMediaTypeOrNull()),
        @Part files: List<MultipartBody.Part>,
    ): BaseResponseData<List<UploadFile>>
}

